package com.an.blog.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.an.blog.admin.domain.bo.BlogFriendLinkBo;
import com.an.blog.admin.domain.vo.BlogFriendLinkVo;
import com.an.blog.service.IBlogFriendLinkService;
import com.an.common.annotation.Log;
import com.an.common.annotation.RepeatSubmit;
import com.an.common.core.controller.BaseController;
import com.an.common.core.domain.PageQuery;
import com.an.common.core.domain.R;
import com.an.common.core.page.TableDataInfo;
import com.an.common.core.validate.AddGroup;
import com.an.common.core.validate.EditGroup;
import com.an.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;

/**
 * 博客友链
 *
 * @author Ann
 * @date 2023-10-17
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/blog/friendLink")
public class BlogFriendLinkController extends BaseController {

    private final IBlogFriendLinkService blogFriendLinkService;

    /**
     * 查询博客友链列表
     */
    @SaCheckPermission("blog:friendLink:list")
    @GetMapping("/list")
    public TableDataInfo<BlogFriendLinkVo> list(BlogFriendLinkBo bo, PageQuery pageQuery) {
        return blogFriendLinkService.queryPageList(bo, pageQuery);
    }

    /**
     * 获取博客友链详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("blog:friendLink:query")
    @GetMapping("/{id}")
    public R<BlogFriendLinkVo> getInfo(@NotNull(message = "主键不能为空")
                                       @PathVariable Long id) {
        return R.ok(blogFriendLinkService.queryById(id));
    }

    /**
     * 新增博客友链
     */
    @SaCheckPermission("blog:friendLink:add")
    @Log(title = "博客友链", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody BlogFriendLinkBo bo) {
        return toAjax(blogFriendLinkService.insertByBo(bo));
    }

    /**
     * 修改博客友链
     */
    @SaCheckPermission("blog:friendLink:edit")
    @Log(title = "博客友链", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody BlogFriendLinkBo bo) {
        return toAjax(blogFriendLinkService.updateByBo(bo));
    }

    /**
     * 删除博客友链
     *
     * @param ids 主键串
     */
    @SaCheckPermission("blog:friendLink:remove")
    @Log(title = "博客友链", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(blogFriendLinkService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 批量审核方法
     *
     * @param ids 主键串
     */
    @SaCheckPermission("blog:friendLink:edit")
    @Log(title = "博客友链", businessType = BusinessType.UPDATE)
    @PutMapping("/check/{ids}")
    public R<Void> check(@NotEmpty(message = "主键不能为空")
                         @PathVariable Long[] ids) {
        return toAjax(blogFriendLinkService.checkByIds(Arrays.asList(ids)));
    }


    /**
     * 取消检查
     *
     * @param id id
     * @return {@link R }<{@link Void }>
     * @author: Ann
     * @createDate: 2023/10/17 15:06:02
     * @version: 1.0.0
     */
    @SaCheckPermission("blog:friendLink:edit")
    @Log(title = "博客友链", businessType = BusinessType.UPDATE)
    @PutMapping("/cancelCheck/{id}")
    public R<Void> cancelCheck(@PathVariable Long id) {
        return toAjax(blogFriendLinkService.cancelCheck(id));
    }
}
